<?php
declare (strict_types=1);

namespace app\backend\controller\v4;

use app\backend\validate\LoginValidate;
use think\exception\ValidateException;
use app\common\models\ApiMember as ApiMemberModel;

class ApiSite extends BaseController
{
    /**
     * 注册
     * account：账号
     * password：mima
     */
    public function register()
    {
        $postData = input('post.');
        try {
            //  验证参数
            validate(LoginValidate::class)->scene('register')->check($postData);
            $user = new ApiMemberModel();
            $memberInfo = $user->fetchInfoByWhere(['account' => $postData['account']], 'id,account');
            if (!empty($memberInfo)) {
                return errMsg(422, '账号已存在！');
            }
            $postData['salt'] = getRandStr(6);
            $postData['password_hash'] = makePassword($postData['password'], $postData['salt']);

            if ($postData['past_time'] <= date('Y-m-d H:i:s',time())) {
                return errMsg(400, '请输入正确的期限！');
            }
            if ($user->save($postData)) {
                return sucessMsg(200, '注册成功');
            }
            return errMsg(422, '注册失败，请重试！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }
}
